package com.copyctrl.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.copyctrl.admin.dto.PassageDTO;
import com.copyctrl.admin.service.PassageService;
import com.copyctrl.campus.common.constant.RedisConstant;
import com.copyctrl.feignapi.client.SearchClient;
import com.copyctrl.mapper.CampCommentMapper;
import com.copyctrl.mapper.CampPassageMapper;
import com.copyctrl.model.CampCommentExample;
import com.copyctrl.model.CampPassage;
import com.copyctrl.model.CampPassageExample;
import com.copyctrl.publisher.send.DataPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class PassageServiceImpl implements PassageService {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    CampPassageMapper campPassageMapper;

    @Autowired
    DataPublisher dataPublisher;

    @Autowired
    CampCommentMapper mapper;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Autowired
    SearchClient client;
    @Override
    public int addPassage(CampPassage passage) {
        //往数据库里插入数据，成功存入redis，由于管理界面用户较少，不再做异步操作。
        passage.setHot(0);
        passage.setCreateTime(dateFormat.format(new Date()));
        passage.setCommentCount(0);
        int insert = campPassageMapper.insert(passage);
        if(insert==1){
            PassageDTO passageDTO = BeanUtil.copyProperties(passage, PassageDTO.class);
            System.out.println(passageDTO);
            long score = 0;
            try {
                score = dateFormat.parse(passage.getCreateTime()).getTime()+passage.getId();
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            String json = JSONUtil.toJsonStr(passageDTO);
            redisTemplate.opsForZSet().add(RedisConstant.PASSAGE_ALL, json, score);
            redisTemplate.opsForZSet().add(RedisConstant.PASSAGE_KIND+passage.getKind(), json, score);
            HashMap<String, String> stringStringHashMap = new HashMap<>();
            stringStringHashMap.put("passage",json);
            client.add(stringStringHashMap);
        }
        return insert;
    }

    @Override
    public int updatePassage(CampPassage passage,PassageDTO old) {
        CampPassageExample example=new CampPassageExample();
        example.createCriteria().andIdEqualTo(passage.getId());
        int i = campPassageMapper.updateByExampleSelective(passage, example);
        if(i==1){
            //redis删除原来的文章详情信息
            redisTemplate.delete(RedisConstant.PASSAGE_KEY+old.getId());
            PassageDTO passageDTO = BeanUtil.copyProperties(passage, PassageDTO.class);
            long score = 0;
            try {
                score = dateFormat.parse(passage.getCreateTime()).getTime()+passage.getId();
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            String toJsonStr = JSONUtil.toJsonStr(old);
            System.out.println(toJsonStr);
            Long remove = redisTemplate.opsForZSet().remove(RedisConstant.PASSAGE_ALL, toJsonStr);
            Long remove1 = redisTemplate.opsForZSet().remove(RedisConstant.PASSAGE_KIND + old.getKind(), toJsonStr);
            System.out.println("remove="+remove+",remove1="+remove1);
            String jsonStr = JSONUtil.toJsonStr(passageDTO);
            redisTemplate.opsForZSet().add(RedisConstant.PASSAGE_ALL, jsonStr, score);
            redisTemplate.opsForZSet().add(RedisConstant.PASSAGE_KIND+passage.getKind(), jsonStr,score);
            client.update(jsonStr);
        }
        return i;
    }

    @Override
    public int deletePassage(PassageDTO passageDTO) {
        System.out.println(passageDTO);
        Integer passageId = passageDTO.getId();
        int i = campPassageMapper.deleteByPrimaryKey(passageId);
        if(i==1){
            redisTemplate.delete(RedisConstant.PASSAGE_KEY+passageId);
            redisTemplate.delete(RedisConstant.COMMENT_KEY+passageId);
            String s = JSONUtil.toJsonStr(passageDTO);
            //删除es中数据
            client.delete(s);
            System.out.println(s);
            redisTemplate.opsForZSet().remove(RedisConstant.PASSAGE_ALL, s);
            Integer kind = passageDTO.getKind();
            redisTemplate.opsForZSet().remove(RedisConstant.PASSAGE_KIND+kind, s);
            //删除评论
            CampCommentExample example = new CampCommentExample();
            example.createCriteria().andTextIdEqualTo(passageId);
            mapper.deleteByExample(example);
        }
        return i;
    }

    @Override
    public List<CampPassage> getallPassages() {
        CampPassageExample example=new CampPassageExample();
        example.createCriteria().andIdIsNotNull();
        List<CampPassage> campPassages = campPassageMapper.selectByExample(example);
        return campPassages;
    }

    @Override
    public List<CampPassage> getByKind(String kind) {
        CampPassageExample example=new CampPassageExample();
        example.createCriteria().andKindEqualTo(Integer.valueOf(kind));
        List<CampPassage> campPassages = campPassageMapper.selectByExample(example);
        return campPassages;
    }
}
